Yes, this would work.
The reversed String could be built up character by character in an array.
Since arrays can be altered at any time,
only one new object would
be constructed.
This is essentially the idea of the StringBuffer
class.
A StringBuffer
object holds a String of characters that
can be changed by appending characters at the end and by inserting characters.
However, unlike a char array, a StringBuffer
comes with
many methods convenient for character manipulation.
A StringBuffer
automatically grows in length as needed.
Constructors for StringBuffer
are:
StringBuffer constructors | |
---|---|
public StringBuffer() | create an empty StringBuffer |
public StringBuffer(int capacity) | create a StringBuffer with initial room for capacity characters |
public StringBuffer(String st) | create a StringBuffer containing the characters from st |